home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / GCC257RM.ZIP / utils / gcc-rm / glob.c < prev    next >
C/C++ Source or Header  |  1993-11-29  |  3KB  |  183 lines

  1. #include <stdio.h>
  2. #include <alloc.h>
  3. #include <dir.h>
  4. #include <dos.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8. static char **newargv;
  9.  
  10. static int fscan_q(FILE *f, char *buf)
  11. {
  12.   char *ibuf = buf;
  13.   int c, quote=-1, gotsome=0, addquote=0;
  14.   while ((c = fgetc(f)) != EOF)
  15.   {
  16.     if (c == '\\')
  17.     {
  18.       char c2 = fgetc(f);
  19.       if (! strchr("\"'`\\ \t\n\r", c2))
  20.         *buf++ = c;
  21.       *buf++ = c2;
  22.       addquote = 0;
  23.     }
  24.     else if (c == quote)
  25.     {
  26.       quote = -1;
  27.       if (c == '\'')
  28.         addquote = 1;
  29.     }
  30.     else if (isspace(c) && (quote==-1))
  31.     {
  32.       if (gotsome)
  33.       {
  34.         if (addquote)
  35.           *buf++ = '\'';
  36.         *buf = 0;
  37.         return 1;
  38.       }
  39.       addquote = 0;
  40.     }
  41.     else
  42.     {
  43.       if ((quote == -1) && ((c == '"') || (c == '\'')))
  44.       {
  45.         quote = c;
  46.         gotsome=1;
  47.         if ((c == '\'') && (buf == ibuf))
  48.           *buf++ = c;
  49.       }
  50.       else
  51.       {
  52.         *buf++ = c;
  53.         gotsome=1;
  54.       }
  55.       addquote = 0;
  56.     }
  57.   }
  58.   return 0;
  59. }
  60.  
  61. static glob(char *buf, int (*func)())
  62. {
  63.   if (strpbrk(buf, "*?"))
  64.   {
  65.     char *dire, *cp;
  66.     struct ffblk ff;
  67.     int done, upcase=0;
  68.     done = findfirst(buf, &ff, FA_RDONLY|FA_DIREC|FA_ARCH);
  69.     if (done)
  70.       func(buf);
  71.     else
  72.     {
  73.       char nbuf[80];
  74.       strcpy(nbuf, buf);
  75.       for (dire=cp=nbuf; *cp; cp++)
  76.       {
  77.         if (strchr("/\\:", *cp))
  78.           dire = cp + 1;
  79.         if (isupper(*cp))
  80.           upcase = 1;
  81.       }
  82.       while (!done)
  83.       {
  84.         strcpy(dire, ff.ff_name);
  85.         if (!upcase)
  86.           strlwr(dire);
  87.         func(nbuf);
  88.         done = findnext(&ff);
  89.       }
  90.     }
  91.   }
  92.   else
  93.     func(buf);
  94. }
  95.  
  96. static foreach_arg(char **argv, int (*func)())
  97. {
  98.   int i;
  99.   char firstc;
  100.   FILE *f;
  101.   char buf[80];
  102.   for (i=0; argv[i]; i++)
  103.   {
  104.     if (argv[i][0] == '@')
  105.     {
  106.       f = fopen(argv[i]+1, "rt");
  107.       while (fscan_q(f, buf) == 1)
  108.       {
  109.         if (!strcmp(buf, "\032"))
  110.           continue;
  111.         glob(buf, func);
  112.       }
  113.       fclose(f);
  114.     }
  115.     else
  116.       glob(argv[i], func);
  117.   }
  118. }
  119.  
  120. static int num_actual_args;
  121.  
  122. static just_incr()
  123. {
  124.   num_actual_args++;
  125. }
  126.  
  127. static pusharg(char *ar)
  128. {
  129.   int s = strlen(ar);
  130.   if ((ar[0] == '\'') && (ar[s-1] == '\''))
  131.   {
  132.     ar[s-1] = '\0';
  133.     ar++;
  134.   }
  135.   newargv[num_actual_args] = strdup(ar);
  136.   num_actual_args++;
  137. }
  138.  
  139. static getlongargs(int *ac, char ***av)
  140. {
  141.   char *_argcs = getenv("_argc");
  142.   int _argc, i;
  143.   char tmp[10];
  144.   char **_argv;
  145.  
  146.   if (_argcs == 0)
  147.     return;
  148.   if (*ac > 1)
  149.     return;
  150.  
  151.   _argc = atoi(_argcs);
  152.   _argv = (char **)malloc((_argc+1)*sizeof(char*));
  153.   for (i = 0; i<_argc; i++)
  154.   {
  155.     sprintf(tmp, "_argv%d", i);
  156.     _argv[i] = getenv(tmp);
  157.   }
  158.   _argv[i] = 0;
  159.   *av = _argv;
  160.   *ac = _argc;
  161.   putenv("_argc=");
  162. }
  163.  
  164. set_argc_argv(int *argc, char ***argv)
  165. {
  166.   getlongargs(argc, argv);
  167.   num_actual_args = 0;
  168.   foreach_arg(*argv, just_incr);
  169.  
  170.   newargv = (char **)malloc((num_actual_args+1)*sizeof(char *));
  171.   if (newargv == 0)
  172.   {
  173.     printf("Fatal! no memory to copy arguments\n");
  174.     exit(1);
  175.   }
  176.   num_actual_args = 0;
  177.   foreach_arg(*argv, pusharg);
  178.   newargv[num_actual_args] = 0;
  179.  
  180.   *argv = newargv;
  181.   *argc = num_actual_args;
  182. }
  183.